Imports System.Data.SqlClient
Public Class cls_diametro_

    Implements ICloneable

#Region "Variables ..."
    Private int_iddiametro As Integer = 0
    Private dbl_valor As Double = 0
    Private str_equivalencia1 As String = ""
    Private str_equivalencia2 As String = ""
    Private str_equivalencia3 As String = ""
    Private str_equivalencia4 As String = ""
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_ = Nothing
    Private col_Items As Collections.Generic.List(Of cls_diametro_) = Nothing
    Public Overrides Function ToString() As String
        Return valor
    End Function
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return iddiametro
        End Get
    End Property
    Property iddiametro() As Integer
        Get
            iddiametro = int_iddiametro
        End Get
        Set(ByVal value As Integer)
            int_iddiametro = value
        End Set
    End Property
    Property valor() As Double
        Get
            valor = dbl_valor
        End Get
        Set(ByVal value As Double)
            dbl_valor = value
        End Set
    End Property
    Property equivalencia1() As String
        Get
            equivalencia1 = str_equivalencia1
        End Get
        Set(ByVal value As String)
            str_equivalencia1 = value
        End Set
    End Property
    Property equivalencia2() As String
        Get
            equivalencia2 = str_equivalencia2
        End Get
        Set(ByVal value As String)
            str_equivalencia2 = value
        End Set
    End Property
    Property equivalencia3() As String
        Get
            equivalencia3 = str_equivalencia3
        End Get
        Set(ByVal value As String)
            str_equivalencia3 = value
        End Set
    End Property
    Property equivalencia4() As String
        Get
            equivalencia4 = str_equivalencia4
        End Get
        Set(ByVal value As String)
            str_equivalencia4 = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_diametro_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_diametro_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_diametro_)
        While obj_Reader.Read()
            Dim Elemento As New cls_diametro_
            Elemento.int_iddiametro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddiametro")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
                Elemento.dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia1"))) = False Then
                Elemento.str_equivalencia1 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia1")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia2"))) = False Then
                Elemento.str_equivalencia2 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia2")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia3"))) = False Then
                Elemento.str_equivalencia3 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia3")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia4"))) = False Then
                Elemento.str_equivalencia4 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia4")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_iddiametro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddiametro")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
            dbl_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia1"))) = False Then
            str_equivalencia1 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia1")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia2"))) = False Then
            str_equivalencia2 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia2")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia3"))) = False Then
            str_equivalencia3 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia3")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia4"))) = False Then
            str_equivalencia4 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("equivalencia4")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__Nuevo"
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@equivalencia1", SqlDbType.NVarChar).Value = str_equivalencia1
                .Parameters.Add("@equivalencia2", SqlDbType.NVarChar).Value = str_equivalencia2
                .Parameters.Add("@equivalencia3", SqlDbType.NVarChar).Value = str_equivalencia3
                .Parameters.Add("@equivalencia4", SqlDbType.NVarChar).Value = str_equivalencia4
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__Modificar"
                .Parameters.Add("@iddiametro", SqlDbType.Int).Value = int_iddiametro
                .Parameters.Add("@valor", SqlDbType.Decimal).Value = dbl_valor
                .Parameters.Add("@equivalencia1", SqlDbType.NVarChar).Value = str_equivalencia1
                .Parameters.Add("@equivalencia2", SqlDbType.NVarChar).Value = str_equivalencia2
                .Parameters.Add("@equivalencia3", SqlDbType.NVarChar).Value = str_equivalencia3
                .Parameters.Add("@equivalencia4", SqlDbType.NVarChar).Value = str_equivalencia4
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal iddiametro As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__Eliminar"
                .Parameters.Add("@iddiametro", SqlDbType.Int).Value = iddiametro
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal iddiametro As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__Buscar"
                .Parameters.Add("@iddiametro", SqlDbType.Int).Value = iddiametro
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "diametro__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de diametro_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_diametro_Clon As New cls_diametro_
        obj_diametro_Clon.int_iddiametro = Me.int_iddiametro
        obj_diametro_Clon.dbl_valor = Me.dbl_valor
        obj_diametro_Clon.str_equivalencia1 = Me.str_equivalencia1
        obj_diametro_Clon.str_equivalencia2 = Me.str_equivalencia2
        obj_diametro_Clon.str_equivalencia3 = Me.str_equivalencia3
        obj_diametro_Clon.str_equivalencia4 = Me.str_equivalencia4
        obj_diametro_Clon.int_idregistro = Me.int_idregistro
        obj_diametro_Clon.col_Items = Me.col_Items
        Return obj_diametro_Clon
    End Function

End Class
